<?php

declare(strict_types=1);

namespace app\middleware;

use app\model\Log as Logger;
use app\utils\Utils as Utils;

class Log
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return \think\Response
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);
        $data = [
            'user_id' => Utils::getCurrentUser() ? Utils::getCurrentUser()['id'] : 0,
            'text' => json_decode($response->getContent())->msg ?? 'Unknown',
            'code' => $response->getCode(),
            'request_data' => json_encode($request->param()),
            'output_data' => Utils::en_emoji($response->getContent()),
            'type' => $request->pathinfo()
        ];
        Logger::WriteData($data);
        return $response;
    }
}
